<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>find 命令概览 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>find 命令概览</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Linux 下 find 命令在目录结构中搜索文件，并执行指定的操作。Linux 下 find 命令提供了相当多的查找条件，功能很强大。由于 find 具有强大的功能，所以它的选项也很多，其中大部分选项都值得我们花时间来了解一下。即使系统中含有网络文件系统( NFS)，find 命令在该文件系统中同样有效，只要具有相应的权限。 在运行一个非常消耗资源的find命令时，很多人都倾向于把它放在后台执行，因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。</p>
<h2 id="1-命令格式"><a href="#1-命令格式" class="headerlink" title="1.命令格式"></a>1.命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">find pathname -options [-<span class="built_in">print</span> -<span class="built_in">exec</span> -ok ...]</div></pre></td></tr></table></figure>
<h2 id="2-命令功能"><a href="#2-命令功能" class="headerlink" title="2.命令功能"></a>2.命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用于在文件树中查找文件，并作出相应的处理</p>
<h2 id="3-命令参数"><a href="#3-命令参数" class="headerlink" title="3.命令参数"></a>3.命令参数</h2><ul>
<li>pathname：find命令所查找的目录路径。例如用.来表示当前目录，用/来表示系统根目录。 </li>
<li>-print：find命令将匹配的文件输出到标准输出。</li>
<li>-exec：find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为’command’ {  } \;，注意{   }和\；之间的空格。</li>
<li>-ok：和-exec的作用相同，只不过以一种更为安全的模式来执行该参数所给出的shell命令，在执行每一个命令之前，都会给出提示，让用户来确定是否执行。</li>
</ul>
<h2 id="4-命令选项"><a href="#4-命令选项" class="headerlink" title="4.命令选项"></a>4.命令选项</h2><ul>
<li>-name   按照文件名查找文件。</li>
<li>-perm   按照文件权限来查找文件。</li>
<li>-prune  使用这一选项可以使find命令不在当前指定的目录中查找，如果同时使用-depth选项，那么-prune将被find命令忽略。</li>
<li>-user   按照文件属主来查找文件。</li>
<li>-group  按照文件所属的组来查找文件。</li>
<li>-mtime -n +n  按照文件的更改时间来查找文件， - n表示文件更改时间距现在n天以内，+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项，但它们都和-m time选项。</li>
<li>-nogroup  查找无有效所属组的文件，即该文件所属的组在/etc/groups中不存在。</li>
<li>-nouser   查找无有效属主的文件，即该文件的属主在/etc/passwd中不存在。</li>
<li>-newer file1 ! file2  查找更改时间比文件file1新但比文件file2旧的文件。</li>
<li>-type  查找某一类型的文件，诸如：</li>
<li>b - 块设备文件。</li>
<li>d - 目录。</li>
<li>c - 字符设备文件。</li>
<li>p - 管道文件。</li>
<li>l - 符号链接文件。</li>
<li>f - 普通文件。</li>
<li>-size n：[c] 查找文件长度为n块的文件，带有c时表示文件长度以字节计。-depth：在查找文件时，首先查找当前目录中的文件，然后再在其子目录中查找。</li>
<li>-fstype：查找位于某一类型文件系统中的文件，这些文件系统类型通常可以在配置文件/etc/fstab中找到，该配置文件中包含了本系统中有关文件系统的信息。</li>
<li>-mount：在查找文件时不跨越文件系统mount点。</li>
<li>-follow：如果find命令遇到符号链接文件，就跟踪至链接所指向的文件。</li>
<li>-cpio：对匹配的文件使用cpio命令，将这些文件备份到磁带设备中。</li>
</ul>
<h3 id="另外-下面三个的区别"><a href="#另外-下面三个的区别" class="headerlink" title="另外,下面三个的区别:"></a>另外,下面三个的区别:</h3><ul>
<li>-amin n   查找系统中最后N分钟访问的文件</li>
<li>-atime n  查找系统中最后n*24小时访问的文件</li>
<li>-cmin n   查找系统中最后N分钟被改变文件状态的文件</li>
<li>-ctime n  查找系统中最后n*24小时被改变文件状态的文件</li>
<li>-mmin n   查找系统中最后N分钟被改变文件数据的文件</li>
<li>-mtime n  查找系统中最后n*24小时被改变文件数据的文件</li>
</ul>
<h2 id="使用实例"><a href="#使用实例" class="headerlink" title="使用实例"></a>使用实例</h2><h3 id="实例1：查找指定时间内修改过的文件"><a href="#实例1：查找指定时间内修改过的文件" class="headerlink" title="实例1：查找指定时间内修改过的文件"></a>实例1：查找指定时间内修改过的文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">find -atime -2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@peidachang ~]<span class="comment"># find -atime -2</span></div><div class="line">.</div><div class="line">./logs/monitor</div><div class="line">./.bashrc</div><div class="line">./.bash_profile</div><div class="line">./.bash_history</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查找 48 小时内修改过的文件</p>
<h3 id="实例2：根据关键字查找"><a href="#实例2：根据关键字查找" class="headerlink" title="实例2：根据关键字查找"></a>实例2：根据关键字查找</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">find . -name <span class="string">"*.log"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># find . -name "*.log" </span></div><div class="line">./log_link.log</div><div class="line">./log2014.log</div><div class="line">./test4/log3-2.log</div><div class="line">./test4/log3-3.log</div><div class="line">./test4/log3-1.log</div><div class="line">./log2013.log</div><div class="line">./log2012.log</div><div class="line">./log.log</div><div class="line">./test5/log5-2.log</div><div class="line">./test5/log5-3.log</div><div class="line">./test5/log.log</div><div class="line">./test5/log5-1.log</div><div class="line">./test5/test3/log3-2.log</div><div class="line">./test5/test3/log3-3.log</div><div class="line">./test5/test3/log3-1.log</div><div class="line">./test3/log3-2.log</div><div class="line">./test3/log3-3.log</div><div class="line">./test3/log3-1.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在当前目录查找以 .log 结尾的文件。“.” 代表当前目录</p>
<h3 id="实例3：按照目录或文件的权限来查找文件"><a href="#实例3：按照目录或文件的权限来查找文件" class="headerlink" title="实例3：按照目录或文件的权限来查找文件"></a>实例3：按照目录或文件的权限来查找文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">find /opt/soft/<span class="built_in">test</span>/ -perm 777</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># find /opt/soft/test/ -perm 777</span></div><div class="line">/opt/soft/<span class="built_in">test</span>/log_link.log</div><div class="line">/opt/soft/<span class="built_in">test</span>/test4</div><div class="line">/opt/soft/<span class="built_in">test</span>/test5/test3</div><div class="line">/opt/soft/<span class="built_in">test</span>/test3</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查找 /opt/soft/test/ 目录下，权限为 777 的文件</p>
<h3 id="实例4：按类型查找"><a href="#实例4：按类型查找" class="headerlink" title="实例4：按类型查找"></a>实例4：按类型查找</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">find . -<span class="built_in">type</span> f -name <span class="string">"*.log"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># find . -type f -name "*.log"</span></div><div class="line">./log2014.log</div><div class="line">./test4/log3-2.log</div><div class="line">./test4/log3-3.log</div><div class="line">./test4/log3-1.log</div><div class="line">./log2013.log</div><div class="line">./log2012.log</div><div class="line">./log.log</div><div class="line">./test5/log5-2.log</div><div class="line">./test5/log5-3.log</div><div class="line">./test5/log.log</div><div class="line">./test5/log5-1.log</div><div class="line">./test5/test3/log3-2.log</div><div class="line">./test5/test3/log3-3.log</div><div class="line">./test5/test3/log3-1.log</div><div class="line">./test3/log3-2.log</div><div class="line">./test3/log3-3.log</div><div class="line">./test3/log3-1.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查找当前目录，以 .log 结尾的普通文件</p>
<h3 id="实例5：查找当前所有目录并排序"><a href="#实例5：查找当前所有目录并排序" class="headerlink" title="实例5：查找当前所有目录并排序"></a>实例5：查找当前所有目录并排序</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">find .-<span class="built_in">type</span> d |sort</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># find . -type d | sort</span></div><div class="line">.</div><div class="line">./scf</div><div class="line">./scf/bin</div><div class="line">./scf/doc</div><div class="line">./scf/lib</div><div class="line">./scf/service</div><div class="line">./scf/service/deploy</div><div class="line">./scf/service/deploy/info</div><div class="line">./scf/service/deploy/product</div><div class="line">./test3</div><div class="line">./test4</div><div class="line">./test5</div><div class="line">./test5/test3</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例6：按大小查找文件"><a href="#实例6：按大小查找文件" class="headerlink" title="实例6：按大小查找文件"></a>实例6：按大小查找文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">find . -size +1000c -<span class="built_in">print</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#  find . -size +1000c -print</span></div><div class="line">.</div><div class="line">./test4</div><div class="line">./scf</div><div class="line">./scf/lib</div><div class="line">./scf/service</div><div class="line">./scf/service/deploy</div><div class="line">./scf/service/deploy/product</div><div class="line">./scf/service/deploy/info</div><div class="line">./scf/doc</div><div class="line">./scf/bin</div><div class="line">./log2012.log</div><div class="line">./test5</div><div class="line">./test5/test3</div><div class="line">./test3</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查找当前目录大于1K的文件</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/19. find 命令概览/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
